package com.wd.common.http

import android.content.Context
import android.net.ConnectivityManager
import com.wd.common.api.ApiService
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.create
import java.util.concurrent.TimeUnit

/**
 * 作者:孙殿彭 by mac on2023/9/5 09:34
 * 类注释:
 */
object HttpUtils {

    private var httpUtils:HttpUtils?=null
    val apiService:ApiService

    init {
        val okHttpClient=OkHttpClient.Builder()
            .connectTimeout(5000,TimeUnit.MILLISECONDS)
            .readTimeout(5000,TimeUnit.MILLISECONDS)
            .addNetworkInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
            .build()
        val retrofit=Retrofit.Builder()
            .client(okHttpClient)
            .baseUrl("http://10.59.9.18/")
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create())
            .build()
        apiService=retrofit.create(ApiService::class.java)
    }
    @Synchronized
    fun getInstance():HttpUtils{
        if(httpUtils==null){
            httpUtils = HttpUtils
        }
        return httpUtils!!
    }
    fun isHttp(context: Context):Boolean{
        val conn=context.getSystemService(Context.CONNECTIVITY_SERVICE)as ConnectivityManager
        val info=conn.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
        return info!=null&&info.isConnected

    }


}